﻿namespace Vcare.Controllers
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Entity;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using Vcare.Models;
    using Vcare.DAL;
    using System.IO;
    using System.Web.Security;
    using System.Data.Entity.Validation;
    using Vcare.Helpers;
    using System.Configuration;

    public class RatesController : Controller
    {
        private VcareContext db = new VcareContext();

        public ActionResult Index(int callCentreID=0)
        {
          List<Rates> listRates = new List<Rates>();
          if (callCentreID!=0)
          {
            listRates = db.Rates.Where(m => m.CallCentreID == callCentreID).ToList();
            ViewBag.CallCentre = db.CallCentres.Find(callCentreID).potitle;
            ViewBag.CallCentreID = callCentreID;
          }
          else
          {
            listRates = db.Rates.Where(m => m.CallCentreID == callCentreID).ToList();
          }
            return View(listRates);
        }

        //
        // GET: /Rates/Details/5

        public ActionResult Details(int id)
        {
          
            return View();
        }

        //
        // GET: /Rates/Create

        public ActionResult Create(int callCentreID)
        {
          ViewBag.CallCentre = db.CallCentres.Find(callCentreID).CallCentre;
          return View();
        }

        //
        // POST: /Rates/Create

        [HttpPost]
        public ActionResult Create(Rates rates, string period)
        {
            try
            {
              if (ModelState.IsValid)
              {
                rates = FetchPeriod(rates, period);     
                db.Rates.Add(rates);
                db.SaveChanges();
              }

              return RedirectToAction("Index", new { callCentreID = rates.CallCentreID});
            }
            catch
            {
                return View();
            }
        }

        public Rates FetchPeriod(Rates rate, string period)
        {
          switch (period)
          {
            case "Annual":
              rate.Annual = true;
              break;
            case "Quarterly":
              rate.Quarterly = true;
              break;
            case "Monthly":
              rate.Monthly = true;
              break;
            default:
              break;
          }

          return rate;
        }

        //
        // GET: /Rates/Edit/5

        public ActionResult Edit(int id)
        {
          Rates rates=db.Rates.Find(id);
          ViewBag.CallCentre = db.CallCentres.Find(rates.CallCentreID).CallCentre;
          return View(rates);
        }

        //
        // POST: /Rates/Edit/5

        [HttpPost]
        public ActionResult Edit(Rates rate, string period)
        {
            try
            {
              rate = FetchPeriod(rate, period);     
              db.Entry(rate).State = EntityState.Modified;
              db.SaveChanges();

              return RedirectToAction("Index", new { callCentreID=rate.CallCentreID});
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Rates/Delete/5

        public ActionResult Delete(int id)
        {
          Rates rates = db.Rates.Find(id);
          return View(rates);
        }

        //
        // POST: /Rates/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
              Rates rates=db.Rates.Find(id);
              db.Rates.Remove(rates);
              return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
